package com.dcjt.firehotcarpad.model

import android.app.Activity
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.aries.library.fast.manager.LoggerManager
import com.aries.library.fast.retrofit.FastObserver
import com.aries.library.fast.retrofit.FastTransformer
import com.aries.library.fast.util.ToastUtil
import com.dcjt.firecommand.entity.*
import com.dcjt.firehotcarpad.entity.BaseEntity
import com.dcjt.firehotcarpad.entity.DictEntity
import com.dcjt.firehotcarpad.retrofit1.repository.ApiRepository1
import com.google.gson.Gson
import com.trello.rxlifecycle2.android.ActivityEvent
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity

class ScreeningViewModel(app: Application) : AndroidViewModel(app) {
    val requestStatusLiveData: MutableLiveData<BaseEntity<List<DictEntity>>> = MutableLiveData()
    val baseBean = BaseEntity<List<DictEntity>>()
    val uploadBean = BaseEntity<Any>()
    val updateLiveData: MutableLiveData<BaseEntity<Any>> = MutableLiveData()

    fun getDictList(code: String, mContext: Activity){
        ApiRepository1.getInstance().getDictList( code)
            .compose((mContext as RxAppCompatActivity).bindUntilEvent(ActivityEvent.DESTROY))
            .subscribe(object : FastObserver<List<DictEntity>>() {


                override fun _onError(errorRes: Int, errorCode: Int, e: Throwable) {
                    super._onError(errorRes, errorCode, e)
                    baseBean.code =  BaseEntity.err
                    baseBean.data = null
                    requestStatusLiveData.value = baseBean

                }

                override fun _onNext(entity: List<DictEntity>) {
                    LoggerManager.d(Gson().toJson(entity))

                    baseBean.code = BaseEntity.ok
                    baseBean.data = entity
                    requestStatusLiveData.value = baseBean
                }

            })

    }
    fun updateCSLX(code: String, mContext: Activity){
        ApiRepository1.getInstance().updateCSLX( code)
            .compose((mContext as RxAppCompatActivity).bindUntilEvent(ActivityEvent.DESTROY))
            .subscribe(object : FastObserver<Any>() {


                override fun _onError(errorRes: Int, errorCode: Int, e: Throwable) {
                    super._onError(errorRes, errorCode, e)
                    uploadBean.code =  BaseEntity.err
                    uploadBean.data = null
                    updateLiveData.value = uploadBean

                }

                override fun _onNext(entity: Any) {
                    LoggerManager.d(Gson().toJson(entity))

                    uploadBean.code = BaseEntity.ok
                    updateLiveData.value = uploadBean
                }

            })

    }
}